﻿using SpaceNeedle.City6473.Web.Data.Models;
using SpaceNeedle.SDK.Web.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpaceNeedle.City6473.Web.Data.Repositories
{
    public class SettingsRepository : EntitiyFrameworkRepository<SettingsItem>
    {
        public SettingsRepository() : base(new City6473Entities())
        {
            this.dataContext.Configuration.ProxyCreationEnabled = false;
        }

        public SettingsItem Get(string key)
        {
            return this.GetAll().Where(x => x.Key == key).SingleOrDefault();
        }

        public SettingsItem Set(string key, string value)
        {
            if(string.IsNullOrEmpty(key))
            {
                return null;
            }

            SettingsItem settingItem = this.Get(key);

            if (settingItem == null)
            {
                settingItem = new SettingsItem();
                settingItem.Key = key;
                settingItem.Value = value;

                this.Insert(settingItem);
            }
            else
            {
                settingItem.Value = value;
                this.Update(settingItem);
            }

            return settingItem;
        }
    }
}
